<%@page import="com.feib.soeasy.model.Group"%>
<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.model.Payment"%>
<%@page import="com.feib.soeasy.util.CodeTableUtil"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.feib.soeasy.action.Soez5002ActionBean"%>
<%
Soez5002ActionBean actionBean = (Soez5002ActionBean) request.getAttribute("actionBean");
SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
String today = sd.format(Calendar.getInstance().getTime());
%>
<c:choose>
	<c:when test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
	<stripes:form name="emailNotioceForm" id="emailNotioceForm" beanclass="com.feib.soeasy.action.Soez5002ActionBean" method="post" onsubmit="javascript: return false;">
	
		<c:set var="lastPageNumber" value="${actionBean.queryResult.totalPageCount}"/>
		<c:set var="currentPageNumber" value="${actionBean.queryResult.pageNumber}"/>
		<c:set var="prePageNumber" value="${actionBean.queryResult.pageNumber - 1}"/>
		<c:set var="nextPageNumber" value="${actionBean.queryResult.pageNumber + 1}"/>
		
		<div id="paginationBar">
			<input type="button" id="firstPageBtn" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			<input type="button" id="prePageBtn" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			第 <input type="text" id="currentPageText" value="${currentPageNumber }" size="3" maxlength="4"> / ${lastPageNumber} 頁
			<input type="button" id="nextPageBtn" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
			<input type="button" id="lastPageBtn" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
			
		</div>
		
	<c:if test="${null != actionBean.qryPayment && null != actionBean.querySumByPayment }">	
	<c:choose>
	<c:when test="${null ==actionBean.qryPayment.paymentStatus && null ==actionBean.delayCount }">
	
		<div id="paginationBar">
		<table >

	<tr>
		<td align="left">
			<c:out value="${actionBean.querySumByPayment[0]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[0]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[0]['total_amount']}" pattern="##,###" />元		
		</td>
		<td align="center">
			<c:out value="${actionBean.querySumByPayment[1]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[1]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[1]['total_amount']}" pattern="##,###" />元		
		</td>
		<td align="right">
			<c:out value="${actionBean.querySumByPayment[2]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[2]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[2]['total_amount']}" pattern="##,###" />元		
		</td>		
	</tr>
	<tr>	
		<td align="left">	
			<c:out value="${actionBean.querySumByPayment[3]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[3]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[3]['total_amount']}" pattern="##,###" />元		
		</td>
		<td align="center">
			<c:out value="${actionBean.querySumByPayment[4]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[4]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[4]['total_amount']}" pattern="##,###" />元		
		</td>
		<td align="right">
			<c:out value="${actionBean.querySumByPayment[5]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[5]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[5]['total_amount']}" pattern="##,###" />元		
		</td>		
	</tr>

	</table>
			</div>
	</c:when>
	<c:when test="${null !=actionBean.delayCount }">
		<div id="paginationBar">
		<table >

	<tr>
		<td align="left">
			<c:out value="${actionBean.querySumByPayment[0]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[0]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[0]['total_amount']}" pattern="##,###" />元		
		</td>

		<td align="right">
			<c:out value="${actionBean.querySumByPayment[2]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[2]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[2]['total_amount']}" pattern="##,###" />元		
		</td>			
		<td align="left">	
			<c:out value="${actionBean.querySumByPayment[3]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[3]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[3]['total_amount']}" pattern="##,###" />元		
		</td>
		
	</tr>

	</table>
			</div>
	</c:when>
		<c:otherwise>
		<table align="center">
		<tr>
			<td>
			<c:out value="${actionBean.querySumByPayment[0]['payment_Status_Name'] }"/> : <c:out value="${actionBean.querySumByPayment[0]['count'] }"/> 筆/
			金  額: <fmt:formatNumber value="${actionBean.querySumByPayment[0]['total_amount']}" pattern="##,###" />元		
			</td>
		</tr>
		</table>
		</c:otherwise>
	</c:choose>	
	</c:if>	
		
<%
boolean isSchool = false;
try{
	isSchool = Group.GROUP_TYPE_SCHOOL.equals(actionBean.getQueryResult().getPaginatedList().get(0).getGroup().getGroupType());
}
catch(Exception e)
{
    
}
request.setAttribute("isSchool", isSchool);
%>		
		
		<table id="listTable">
		<thead>
			<tr>
				<th>&nbsp;</th>
				
				<th>繳費帳號</th>
				<th>客戶名稱</th>
				<c:choose>
				<c:when test="${isSchool }"><th>學號</th></c:when>
				<c:otherwise><th>客戶編號</th></c:otherwise>
				</c:choose>				
				
				<th>繳費期限</th>
				<th>應繳金額</th>
				<th>繳費日期<br/>銷帳日期</th>
				<th>繳費金額</th>
				<th>繳款狀態</th>
				<th>繳款通路</th>
				<th>&nbsp;</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
			<tr>
				<td align="center">
					<c:choose>
						<c:when test="${2 == viewObj.paymentStatus && null != viewObj.payerEmail}">
							<input name="checkPayment[${rowstat.index}].paymentPk" type="checkbox" id="checkPayment[${rowstat.index}].paymentPk" value="<c:out value='${viewObj.paymentPk}'/>" class="chk"/>
						</c:when>
						<c:otherwise>&nbsp;</c:otherwise>
					</c:choose>	
				</td>
				
				<td align="left"><c:out value="${viewObj.virtualAccount}" /></td>
				<td align="left"><c:out value="${viewObj.payerName}" /></td>
				<td align="left">
				<c:choose>
				<c:when test="${isSchool }"><c:out value="${viewObj.studentId}" /></c:when>
				<c:otherwise><c:out value="${viewObj.payerId}" /></c:otherwise>
				</c:choose>
				
				</td>
				<td align="center"><fmt:formatDate value="${viewObj.payDue}" pattern="yyyy/MM/dd" /></td>
				<td align="right">
					<fmt:formatNumber value="${viewObj.totalAmount}" pattern="##,###" />
				</td>				
				
				<%
				Payment viewObj = (Payment)pageContext.getAttribute("viewObj");
				Integer paymentStatus = viewObj.getPaymentStatus();
				
				String paymentStatusName = CodeTableUtil.getInstance().getCodeName(CodeTableUtil.CODE_KIND_PAYMENT_STATUS, viewObj.getPaymentStatus().toString());
								
				// 判斷是否逾期
				if (Payment.PAYMENT_STATUS_IN_PAYING.equals(viewObj.getPaymentStatus()))
				{
				    String payDueStr = sd.format(viewObj.getPayDue());
				    if (today.compareTo(payDueStr) > 0)
				        paymentStatusName = "逾期";
				}
				
				pageContext.setAttribute("paymentStatusName", paymentStatusName);
				
				
				// 已繳款加入繳款日期時間, 繳款通路代碼
				if (viewObj.getPaymentStatus().equals(new Integer(5)) && null != viewObj.getWriteOffPayment())
				{
				    // 繳款日期時間
				    Date txDate = viewObj.getWriteOffPayment().getTxDate();
				    
				    // 銷帳日期時間
				    Date writeOffDate = viewObj.getWriteOffPayment().getWriteOffDate();
				    
				    // 繳款金額
				    Long writeOffAmount = viewObj.getWriteOffPayment().getAmount();
				    
				    // 繳款通路代碼
				    String writeOffChannel = viewObj.getWriteOffPayment().getChannel();
				    
				    // 繳款通路說明
				    String writeOffChannelName = CodeTableUtil.getInstance().getCodeName(CodeTableUtil.CODE_KIND_CHANNEL, viewObj.getWriteOffPayment().getChannel());

				    pageContext.setAttribute("TxDate", txDate);
				    pageContext.setAttribute("WriteOffDate", writeOffDate);
				    pageContext.setAttribute("writeOffAmount", writeOffAmount);
				    pageContext.setAttribute("writeOffChannel", writeOffChannel);
				    pageContext.setAttribute("writeOffChannelName", writeOffChannelName);
				}
				/* 加入已註銷 繳費日期/繳款日期的顯示 */
				else if(viewObj.getPaymentStatus().equals(new Integer(4)) && null != viewObj.getPaymentCanceled()){
					/* 註銷日期 */
					Date writeOffDate = viewObj.getPaymentCanceled().getCancelDate();
					
					//Date txDate = viewObj.getPaymentCanceled().getCancelDate();
					
					pageContext.setAttribute("WriteOffDate", writeOffDate);
					
					//pageContext.setAttribute("TxDate", txDate);
					
					pageContext.removeAttribute("TxDate");		    
				    pageContext.removeAttribute("writeOffAmount");
				    pageContext.removeAttribute("writeOffChannel");
				    pageContext.removeAttribute("writeOffChannelName");
				}
				else
				{
				    pageContext.removeAttribute("TxDate");
				    pageContext.removeAttribute("WriteOffDate");
				    pageContext.removeAttribute("writeOffAmount");
				    pageContext.removeAttribute("writeOffChannel");
				    pageContext.removeAttribute("writeOffChannelName");
				}
				%>
				
				<td align="center"><fmt:formatDate value="${TxDate}" pattern="yyyy/MM/dd" /><br/><fmt:formatDate value="${WriteOffDate}" pattern="yyyy/MM/dd" /></td>
				<td align="right">
					<fmt:formatNumber value="${writeOffAmount}" pattern="##,###" />
				</td>
				<td align="left">
					<c:choose>
						<c:when test="${2 == viewObj.paymentStatus  &&  paymentStatusName eq '待繳款'}">
							<div id="toWriteoffPaymentBtn-<c:out value='${viewObj.paymentPk}'/>"><a href="javascript: void(0);"><font color="blue"><c:out value="${paymentStatusName}" /></font></a></div>
						</c:when>
						<c:when test="${2 == viewObj.paymentStatus}">
							<div id="toWriteoffPaymentBtn-<c:out value='${viewObj.paymentPk}'/>"><a href="javascript: void(0);"><font color="red"><c:out value="${paymentStatusName}" /></font></a></div>
						</c:when>
						
						<c:otherwise><c:out value="${paymentStatusName}" /></c:otherwise>
					</c:choose>				
				</td>
				<td>
				<c:out value="${writeOffChannelName}" />
				</td>
				<td>
					<div id="viewPaymentImg-<c:out value='${viewObj.paymentPk}'/>"><a href="javascript: void(0)" class="btnZoom">查詢</a></div>
				</td>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	
	<input type="hidden" id="method6" name="doNoticeEmail" />
	
		
		<div id="paginationBar">
			<input type="button" id="checkAll" name="checkAll" value="全選">
			<input type="button" id="unCheckAll" name="unCheckAll" value="全不選">
			<input type="button" id="doNoticeEmailBtn" value="發送Email通知" />
			<input type="button" id="doDownloadExcelFileBtn" value="下載明細資料(Excel)" />
			<input type="button" id="printBtn2" value="列印" />		</div>
		
	</stripes:form>
<%
if (null == actionBean || ! "doAjaxQueryPrint".equalsIgnoreCase(actionBean.getContext().getEventName())){
%>			
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$("[id^='firstPageBtn']").click(function() {
				doQueryAction(1);
			});
			
			$("[id^='prePageBtn']").click(function() {
				doQueryAction(<c:out value='${prePageNumber}'/>);
			});
			
			$("[id^='nextPageBtn']").click(function() {
				doQueryAction(<c:out value='${nextPageNumber}'/>);
			});
			
			$("[id^='lastPageBtn']").click(function() {
				doQueryAction(<c:out value='${lastPageNumber}'/>);
			});
			
			$("[id^='currentPageText']").change(function() {
				var value = this.value;
				if (value != ''){
					value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
					var intRegex = /^\d+$/;
					if(!intRegex.test(value)) {
						alert('頁數必須是整數');
						return ;
					}
					
					value = parseInt(value, 10);
					
					if (value < 1){
						alert('頁數必須是大於1');
						return ;
					}
					else if (value > <c:out value='${lastPageNumber}'/>){
						alert('頁數必須是小於總頁數 (<c:out value='${lastPageNumber}'/>)');
						return ;
					}
					else if (value != <c:out value='${currentPageNumber}'/>){
						doQueryAction(value);
					}
					
				}
				else{
					alert('頁數不可為空白');
					return ;
				}
			});
			
			$('#printBtn2').click(function() {
				//window.print();
				//doQueryPrintAction();
				divPrint('queryResultDiv');//只印目前這筆
			});
			
			$("#checkAll").click(function() {
				doCheckAll();
			});
			
			$("#unCheckAll").click(function() {
				undoCheckAll();
			});
			
			$("#doNoticeEmailBtn").click(function() {
				doNoticeEmailAction(2);
			});
			
			$("#doDownloadExcelFileBtn").click(function() {
				doDownloadExcelFileAction();
			});
			
			$("[id^='viewPaymentImg-']").click(function() {
				var paymentPk = this.id.substring(15);
				doQueryPaymentsAction(paymentPk);
				
			});
			
			$("[id^='toWriteoffPaymentBtn-']").click(function() {
				var paymentPk = this.id.substring(21);
				toWriteoffPaymentAction(paymentPk);				
			});
			
			
		});
		//-->
		</script>

<%   
}
%>
	</c:when>
	<c:otherwise>
	
		<div id="queryNothingBlockDiv" style="display: none;">
			查無資料
		</div>		
		<table id="listTable">
		<thead>
			<tr>
				<th>&nbsp;</th>
				
				<th>繳費帳號</th>
				<th>客戶名稱</th>
				<c:choose>
				<c:when test="${isSchool }"><th>學號</th></c:when>
				<c:otherwise><th>客戶編號</th></c:otherwise>
				</c:choose>				
				
				<th>繳費期限</th>
				<th>應繳金額</th>
				<th>繳費日期<br/>銷帳日期</th>
				<th>繳費金額</th>
				<th>繳款狀態</th>
				<th>繳款通路</th>
				<th>&nbsp;</th>
			</tr>
		</thead>
	
	</table>
	<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$( "#queryNothingBlockDiv" ).dialog({  title:'<%=UserUtil.getCurrentMenuItemName() %>' ,width: 300, resizable: false, modal: true, buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}  });
		});
		//-->
		</script>		
	</c:otherwise>
</c:choose>